package com.honey.solr.daosupport.method;

import com.honey.compilation.java.JavaVisibility;
import com.honey.compilation.java.Method;
import com.honey.compilation.java.MethodDecoration;
import com.honey.compilation.java.Parameter;
import com.honey.core.types.FullyQualifiedJavaType;
import com.honey.core.types.JDKFullyQualifiedJavaType;
import com.honey.generator.java.AbstractClassName;
import com.honey.solr.SolrConstant;
import com.honey.solr.SolrFactory;

public class ConditionAdapterMethod extends AbstractClassName {
	private SolrFactory solrFactory = null;
	
	private String s =""
			+"		StringBuilder query = new StringBuilder();\n"
			+"		List<GeneratedCriteria<GeneratedCriteria<?,?>, Enum<?>>> list = condition.getOredCriteria() ;\n"
			+"		boolean or =false ;\n"
			+"		LuceneQueryAdapter adapter = new LuceneQueryAdapter();\n"
			+"		if( list .size() > 0 ){\n"
			+"			for(GeneratedCriteria gc : list  ){\n"
			+"				boolean and =false ;\n"
			+"				if( or ) query.append(OR_KEYWORD);else or =true;\n"
			+"				query.append('(');\n"
			+"				List<Criterion> l =  gc.getCriteria();\n"
			+"				for(Criterion criterion : l){\n"
			+"					if( and ) query.append(AND_KEYWORD);else and =true ;\n"
			+"					switch ( criterion.getTerm() ){\n"
			+"						case IS_NULL:\n"
			+"						case IS_NOT_NULL:\n"
			+"							break;\n"
			+"							\n"
			+"						case GREATER_THAN:\n"
			+"						case GREATER_THAN_OR_EQUAL_TO:\n"
			+"							query.append( adapter.greater(criterion));\n"
			+"							break;\n"
			+"						\n"
			+"						case LESS_THAN:\n"
			+"						case LESS_THAN_OR_EQUAL_TO:\n"
			+"							query.append( adapter.less(criterion));\n"
			+"							break;\n"
			+"						case IN:\n"
			+"							query.append( adapter.in(criterion));\n"
			+"							break;	\n"
			+"						case NOT_IN:\n"
			+"							query.append( adapter.notIn(criterion));\n"
			+"							break;\n"
			+"						case BETWEEN:\n"
			+"							query.append( adapter.between(criterion));\n"
			+"							break;		\n"
			+"							\n"
			+"						case NOT_BETWEEN:\n"
			+"							query.append( adapter.notBetween(criterion));\n"
			+"							break;\n"
			+"							\n"
			+"						case EQUAL_TO:\n"
			+"						case LIKE:\n"
			+"							query.append( adapter.like(criterion));\n"
			+"							break;\n"
			+"							\n"
			+"						case NOT_EQUAL_TO:\n"
			+"						case NOT_LIKE:\n"
			+"							query.append( adapter.notLike(criterion));\n"
			+"							break;		\n"
			+"					}\n"
			+"				}\n"
			+"				query.append(')');\n"
			+"			}\n"
			+"		}else{\n"
			+"			query.append(\"*:*\");\n"
			+"		}\n"
			+"		return query.toString();\n"
			;
	public ConditionAdapterMethod( SolrFactory solrFactory ){
		this.solrFactory = solrFactory ;
	}
	public Method createMethod(){
		Method answer = new Method(SolrConstant.CONDITION_ADAPTERTO_QUERY);
		answer.addAnnotation("@SuppressWarnings({ \"unchecked\", \"rawtypes\" })");
		MethodDecoration methodDecoration = answer.getDecoration();
		methodDecoration.setVisibility(JavaVisibility.PRIVATE);
		methodDecoration.setFinal(true);
		answer.setReturnType(JDKFullyQualifiedJavaType.getStringInstance());
		Parameter parameter = new Parameter(solrFactory.getConditionType(),"condition");
		answer.addParameter(parameter);
		
		answer.addBodyLine(s);
		return answer ;
	}

}
